package com.qf.controller;

import com.qf.dao.ArchivesDao;
import com.qf.dao.DaoImpl.ArchivesDaoImpl;
import com.qf.pojo.Archives;
import com.qf.pojo.Page;
import com.qf.service.serviceImpl.ArchivesServiceImpl;
import com.qf.util.Constants;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 86198
 * @version V1.0
 * @Project Java_qf
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/27 20:23
 */
@WebServlet("/show")
public class ShowController extends BaseService{
    public String showList(HttpServletRequest request, HttpServletResponse response){
        String pageIndex = request.getParameter("pageIndex");
        System.out.println(pageIndex);

        if(pageIndex==null || pageIndex.equals("")){
            pageIndex="1";
        }
        int pageindex = Integer.parseInt(pageIndex);
        System.out.println(pageindex);
        ArchivesDao archivesDao = new ArchivesDaoImpl();
        ArchivesServiceImpl archivesService = new ArchivesServiceImpl(archivesDao);
        //获取总条数
        int setTotalCounts = archivesService.totalCounts();

        Page page = new Page();
        page.setPage_Index(pageindex);
        page.setPage_number(5);
        page.setPage_start((page.getPage_Index()-1)*page.getPage_number());
        page.setPage_Count(page.getTotalCounts()%page.getPage_number()==0 ? page.getPage_Count()+1 : page.getTotalCounts()/page.getPage_number());
        page.setTotalCounts(setTotalCounts);
        List<Archives> archives = archivesService.showAll(page);
        request.getSession().setAttribute("page",page);
        request.getSession().setAttribute("ArchivesList",archives);
        return  Constants.REDIRECT + request.getContextPath() + "/show.jsp";
    }

    public String update(HttpServletRequest request, HttpServletResponse response){
        Map<String,String[]> map = request.getParameterMap();
        Archives archives = new Archives();
        try {
            BeanUtils.populate(archives,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        System.out.println(archives);
        ArchivesDao archivesDao = new ArchivesDaoImpl();
        ArchivesServiceImpl archivesService = new ArchivesServiceImpl(archivesDao);

        archivesService.update(archives);
        return Constants.REDIRECT + request.getContextPath() + "/show?method=showList";
    }

    public String insertArchives(HttpServletRequest request, HttpServletResponse response){
        String archivesID = request.getParameter("ArchivesID");
        ArchivesDao archivesDao = new ArchivesDaoImpl();
        ArchivesServiceImpl archivesService = new ArchivesServiceImpl(archivesDao);
        Archives archives = archivesService.showOne(Integer.parseInt(archivesID));
        request.getSession().setAttribute("archives",archives);
        return  Constants.REDIRECT + request.getContextPath()+"/insertArchives.jsp";

    }

}
